﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UBlockly.Compotent;

namespace UBlockly.UI
{
    public static class BlockObjectExtension 
    {
        #region BlockObject
        public static void SetColor(this BlockObject block,Color color)
        {
            block.GetComponent<UImage>().color = color;
        }

        public static void SetSprite(this BlockObject block, Texture2D tex)
        {
            block.GetComponent<UImage>().type = UnityEngine.UI.Image.Type.Sliced;
            block.GetComponent<UImage>().pixelsPerUnitMultiplier = 0.5f;
            block.GetComponent<UImage>().sprite = Sprite.Create(tex, new Rect(Vector2.zero, new Vector2(tex.width, tex.height)), 
                new Vector2(0.5f, 0.6f), 100, 0,
                SpriteMeshType.Tight, new Vector4(40, 12, 20, 12));
        }

        public static void SetSprite(this BlockObject block, Sprite sprite)
        {
            block.GetComponent<UImage>().type = UnityEngine.UI.Image.Type.Sliced;
            block.GetComponent<UImage>().pixelsPerUnitMultiplier = 0.5f;
            block.GetComponent<UImage>().sprite = sprite;
        }
        #endregion
        internal static void SetSprite(this ParamObject param, Sprite sp)
        {
            param.GetComponent<UImage>().sprite = sp;
        }
        internal static void SetColor(this ParamObject param, Color color)
        {
            param.GetComponent<UImage>().color = color;
        }
        #region DescObject
        public static void SetText(this DescObject desc, string text)
        {
            desc.GetComponent<UText>().text = text;
        }
        public static void SetSize(this DescObject desc, Vector2 size)
        {
            desc.GetComponent<UDescAnchor>().SetSize(size);
        }

        public static int GetTextLength(this DescObject desc)
        {
            var utext = desc.GetComponent<UText>();
            return Common.GetFontLen(utext.font, utext.fontSize, utext.text);
        }

        public static void SetFont(this DescObject desc, Font font, int size, Color color)
        {
            var utext = desc.GetComponent<UText>();
            utext.font = font;
            utext.alignment = TextAnchor.MiddleLeft;
            utext.fontSize = size;
            utext.color = color;
        }
        #endregion
    }
}
